package com.example.r2dbc.controller;

import com.example.r2dbc.entity.DemoUser;
import com.example.r2dbc.service.DemoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Random;

/**
 * @author renyr
 * @version 0.1
 * @date 2021/1/12 0012 10:53
 * @since 0.1
 */
@RestController
@RequestMapping("demoUser")
public class DemoUserController {

    @Autowired
    private DemoUserService userService;

    private static final Random RANDOM = new Random(3);

    @PostMapping("saveUser")
    public Mono<DemoUser> saveUser(@RequestBody DemoUser user) {
        return userService.saveUser(user);
    }

    @PostMapping("saveUsers")
    public Flux<DemoUser> saveUsers(@RequestBody List<DemoUser> users) {
        DemoUser demoUser = users.get(0);
        long now = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            DemoUser demoUser1 = new DemoUser();
            demoUser1.setUserStatus(demoUser.getUserStatus());
            demoUser1.setCellphone(demoUser.getCellphone());
            demoUser1.setUsername(demoUser.getUsername() + " " + i);
            demoUser1.setBirthday(demoUser.getBirthday().plusDays(i));
            demoUser1.setGender((byte)RANDOM.nextInt(1));
            demoUser1.setCreateTime(now);
            users.add(demoUser1);
        }
        return userService.saveUsers(users);
    }

    @DeleteMapping("deleteUser")
    public Mono<Void> deleteUser(@RequestParam Integer userId) {
        return userService.deleteUser(userId);
    }

    @GetMapping("findUserList")
    public Flux<DemoUser> findUserList() {
        return userService.findUserList();
    }

    @GetMapping("findUserOne")
    public Mono<DemoUser> findUserOne(@RequestParam Integer userId) {
        return userService.findUserOne(userId);
    }

}
